package com.yiyi.credit.manage.abutment.provider;

import com.yiyi.credit.manage.abutment.common.enums.ProductStatus;
import com.yiyi.credit.manage.abutment.common.enums.ProductType;
import com.yiyi.credit.manage.abutment.common.utils.CommonUtil;
import com.yiyi.credit.manage.model.po.ProductPo;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Map;


public class SubProductProvider {

    public String queryList(Map<String, Object> para) {

        return new SQL() {{
            ProductPo v = (ProductPo) para.get("v");
            SELECT("sp.id AS id, sp.product_no AS productNo, sp.product_name AS subProductName, mp.product_name AS mainProductName," +
                    " sp.closed_period AS closedPeriod, sp.closed_period_unit AS closedPeriodUnit, sp.product_period AS productPeriod," +
                    " sp.product_period_unit AS productPeriodUnit, mp.low_invest_amount AS mainLowInvestAmount, mp.increase_amount AS mainIncreaseAmount," +
                    " mp.add_time AS mainAddTime, sp.publish_time AS publishTime, sp.type AS type, sp.add_time AS addTime," +
                    " mp.high_amount AS highAmount, mp.low_amount AS lowAmount, mp.high_rate AS highRate, mp.low_rate AS lowRate," +
                    " mp.product_low_period AS productLowPeriod, mp.product_high_period AS productHighPeriod, mp.product_period_unit AS mainProductPeriodUnit," +
                    " sp.shelve_time AS shelveTime, sp.to_back_reason AS toBackReason, sp.create_people AS createPeople, sp.`status` AS `status`" +
                    " ");
            FROM("sub_product sp LEFT JOIN main_product mp ON sp.main_product_id = mp.id  ");
            StringBuilder where = new StringBuilder(" 1=1 and sp.is_deleted = 0 ");
            where.append(String.format(" and sp.status <> %s ", ProductStatus.DELETE.getKey()));
            buildWhere(v, where);
            WHERE(where.toString());
            if (!ObjectUtils.isEmpty(v.getOrder())) {
                ORDER_BY(v.getOrder());
            }
        }}.toString();
    }

    private void buildWhere(ProductPo v, StringBuilder where) {
        if (!StringUtils.isEmpty(v.getProductNo())) {
            where.append(String.format(" and LOCATE('%s',sp.product_no) ", v.getProductNo()));
        }
        if (!StringUtils.isEmpty(v.getProductName())) {
            where.append(String.format(" and LOCATE('%s',sp.product_name) ", v.getProductName()));
        }
        if (!CommonUtil.isNull(v.getStatus()) && v.getStatus() != -1) {
            where.append(String.format(" and sp.status = %s ", v.getStatus()));
        }
        if (null != v.getSubstationId() && v.getSubstationId() != 0) {
            where.append(String.format(" and sp.substation_id = %s ", v.getSubstationId()));
        }
        if (!ObjectUtils.isEmpty(v.getType())) {
            ProductType.getEnumByKey(v.getType());
            where.append(String.format(" and sp.type = %s ", v.getType()));
        }
        if (!StringUtils.isEmpty(v.getStatuss())) {
            where.append(String.format(" and sp.status in (%s) ", v.getStatuss()));
        }
        if (!ObjectUtils.isEmpty(v.getMainProductId()) && v.getMainProductId() != -1L) {
            where.append(String.format("and sp.main_product_id = %s", v.getMainProductId()));
        }
    }


    public String queryApplyListByParams(Map<String, Object> param) {

        return new SQL() {{
            ProductPo par = (ProductPo) param.get("param");
            SELECT("mp.id productId,mp.main_product_id mainProductId,mp.substation_id substationId,mp.product_no productNo,mp.product_name productName,mp.type productType, mp.current_inventory currentInventory," +
                    "mp.main_product_id mainProductId,mt.product_name mainProductName,mc.high_inventory_amount highInventoryAmount,mc.alarm_inventory_amount alarmInventoryAmount,mc.id configId,mp.id subProductId");
            FROM("sub_product mp");
            LEFT_OUTER_JOIN("product_application_config mc ON mp.id = mc.sub_product_id");
            LEFT_OUTER_JOIN(" main_product mt ON mp.main_product_id = mt.id");
            StringBuilder where = new StringBuilder(" 1=1 ");
            buildApplyListSpecialWhere(par, where);
            WHERE(where.toString());
            ORDER_BY("mp.add_time DESC");
        }}.toString();

    }

    private void buildApplyListSpecialWhere(ProductPo par, StringBuilder where) {
        where.append(String.format(" and mp.status = %s ", ProductStatus.PUBLISHED.getKey()));
        if (!StringUtils.isEmpty(par.getProductNo())) {
            where.append(" and mp.product_no like concat('%','" + par.getProductNo() + "', '%')");
        }
        if (!StringUtils.isEmpty(par.getProductName())) {
            where.append(" and mp.product_name like concat('%','" + par.getProductName() + "', '%')");
        }
        if (null != par.getSubstationId() && par.getSubstationId() != 0) {
            where.append(String.format(" and mp.substation_id = %s ", par.getSubstationId()));
        }
    }

    public String selectSubProductStock(Map<String, Object> para) {
        return "SELECT SUM(current_inventory) FROM sub_product WHERE status = " + para.get("productStatus");
    }
}
